package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.MaintenancePlanMapper;
import com.one.group.admin.mapper.MaintenanceRecordMapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.MaintenancePlanAddDto;
import com.one.group.model.dto.MaintenancePlanExecuteDto;
import com.one.group.model.dto.MaintenancePlanQueryDto;
import com.one.group.model.dto.MaintenancePlanUpdateDto;
import com.one.group.model.entity.MaintenancePlan;
import com.one.group.model.entity.MaintenanceRecord;
import com.one.group.model.vo.MaintenancePlanVo;
import com.one.group.service.MaintenancePlanService;
import com.one.group.utils.ThrowUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * {@code @Create:}  2025-09-28  14 : 52
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@Service
public class MaintenancePlanServiceImpl extends ServiceImpl<MaintenancePlanMapper, MaintenancePlan> implements MaintenancePlanService {
    @Resource
    private MaintenancePlanMapper maintenancePlanMapper;

    @Resource
    private MaintenanceRecordMapper maintenanceRecordMapper;

    @Override
    public void addSpMaintenancePlan(MaintenancePlanAddDto addDto) {
        MaintenancePlan maintenancePlan = MaintenancePlan.builder().tenantId(Convert.toLong(StpUtil.getExtra("adminTenantId:"))).build();
        BeanUtil.copyProperties(addDto, maintenancePlan, false);
        maintenancePlanMapper.insertSelective(maintenancePlan);
    }

    @Override
    public List<MaintenancePlanVo> listSpMaintenancePlans(MaintenancePlanQueryDto queryDto) {
        queryDto.setTenantId(Convert.toLong(StpUtil.getExtra("tenant_id")));
        List<MaintenancePlanVo> maintenancePlanVoList = maintenancePlanMapper.selectList(queryDto);
        return maintenancePlanVoList;
    }

    @Override
    public void updateSpMaintenancePlan(MaintenancePlanUpdateDto updateDto) {
        TenantManager.ignoreTenantCondition();
        MaintenancePlan maintenancePlan = MaintenancePlan.builder().build();
        BeanUtil.copyProperties(updateDto, maintenancePlan);
        maintenancePlanMapper.update(maintenancePlan);
    }

    @Override
    public void batchDeleteSpMaintenancePlans(List<Long> ids) {

        maintenancePlanMapper.deleteBatchByIds(ids);
    }

    @Override
    public void deleteById(Long id) {

        maintenancePlanMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeSpMaintenancePlan(MaintenancePlanExecuteDto executeDto) {
        //查询保养计划
        QueryWrapper wrapper = new QueryWrapper().eq(MaintenancePlan::getId, executeDto.getId());
        MaintenancePlan maintenancePlan = maintenancePlanMapper.selectOneByQuery(wrapper);
        ThrowUtil.throwIf(maintenancePlan == null, ErrorCode.NOT_FOUND_ERROR);

        //状态改为执行中
        maintenancePlan.setStatus(1);

        maintenancePlanMapper.update(maintenancePlan);

        //添加保养记录
        MaintenanceRecord maintenanceRecord =
                MaintenanceRecord.builder().equipmentId(maintenancePlan.getEquipmentId()).
                        actualCost(executeDto.getActualCost()).maintenancePlanId(executeDto.getId()).executor(executeDto.getExecutor()).maintenanceOrderNumber(UUID.randomUUID().toString() + System.currentTimeMillis()).maintenanceProject(executeDto.getMaintenanceProject()).build();

        maintenanceRecordMapper.insertSelective(maintenanceRecord);

    }
}
